<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles, assignRoles } from '@/api/roles'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      checkList: []
    }
  },
  created() {
    this.getRoles()
    this.getUserDetailById()
  },
  methods: {
    // 获取角色列表
    async getRoles() {
      const { data } = await getRoles()
      this.checkList = data.rows
    },
    closeDialog() {
      this.$emit('update:showDialogRole', false)
    },
    // 根据 父组件传过来的 id 获取员工的角色信息
    async getUserDetailById() {
      const { data } = await getUserDetailById(this.id)
      // console.log(data.roleIds)
      this.roleIds = data.roleIds
    },
    // 清空 角色列表
    resetRoleIds() {
      this.roleIds = []
    },
    // 确定分配权限
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
        this.$message.success(res.message)
        this.$emit('update:showDialogRole', false)
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>
